<?php
/**
 * Clase Base FN para BaseSoDeliveryFN
 *
 * @package    
 * @subpackage BaseSoDeliveryFN
 * @author     hans
 * @version    SVN:: BaseSoDeliveryFN.class.php,v 1.1 2012-05-01 22:33:49 Hans.parra Exp $
 */

require_once('class/im/dlv/BN/SoDeliveryBN.class.php');

class BaseSoDeliveryFN extends SoDeliveryBN
{
  private static $fieldNames = array (
    'Id'=>'dlv_id', 'Tipo'=>'dlv_tipo', 'DlvNum'=>'dlv_dlvNum', 'AlmId'=>'dlv_almId', 'ClienteId'=>'dlv_clienteId', 'ClienteNombre'=>'dlv_clienteNombre', 'Contacto'=>'dlv_contacto', 'DocReferNum'=>'dlv_docReferNum', 'FacturaNum'=>'dlv_facturaNum', 'Moneda'=>'dlv_moneda', 'FechaRequerida'=>'dlv_fechaRequerida', 'FechaProgramada'=>'dlv_fechaProgramada', 'FechaSalida'=>'dlv_fechaSalida', 'Flete'=>'dlv_flete', 'TotalBruto'=>'dlv_totalBruto', 'Neto'=>'dlv_neto', 'Obs'=>'dlv_obs', 'Status'=>'dlv_status', 'CreaUsr'=>'dlv_creaUsr', 'CreaFecha'=>'dlv_creaFecha', 'ModifUsr'=>'dlv_modifUsr', 'ModifFecha'=>'dlv_modifFecha'  
  );

  public function select()
  {
    try
    {
      /*$tamPag = !property_exists($this, 'tamPag')?9999:$this->tamPag;
      $pagina = !property_exists($this, 'page')||$this->page==""?1:$this->page;
      $numeroRegistros = $this->selectNumRows();
      $limitInf = ($pagina-1)*$tamPag;*/

      $where = $this->buildConsult();
      //$orden = $this->buildOrderByField();

      $o_conexion = new BaseDatos();
      $con = $o_conexion->conectar();
      //$sql = "SELECT dlv_id, dlv_tipo, dlv_dlvNum, dlv_almId, dlv_clienteId, dlv_clienteNombre, dlv_contacto, dlv_docReferNum, dlv_facturaNum, dlv_moneda, dlv_fechaRequerida, dlv_fechaProgramada, dlv_fechaSalida, dlv_flete, dlv_totalBruto, dlv_neto, dlv_obs, dlv_status, dlv_creaUsr, dlv_creaFecha, dlv_modifUsr, dlv_modifFecha FROM so_delivery WHERE 1 ".$where." ".$orden."  LIMIT ".$limitInf.",".$tamPag;
      $sql = "SELECT dlv_id, dlv_tipo, dlv_dlvNum, dlv_almId, dlv_clienteId, dlv_clienteNombre, dlv_contacto, dlv_docReferNum, dlv_facturaNum, dlv_moneda, dlv_fechaRequerida, dlv_fechaProgramada, dlv_fechaSalida, dlv_flete, dlv_totalBruto, dlv_neto, dlv_obs, dlv_status, dlv_creaUsr, dlv_creaFecha, dlv_modifUsr, dlv_modifFecha FROM so_delivery WHERE 1 ".$where;
        $result = mysql_query($sql);
      $num_rows = mysql_num_rows($result);
      $a_tmp = array();
      if($num_rows>0){
        while ($o_valor = mysql_fetch_object($result)) {
          $a_tmp[] = $this->insertObjectBN($o_valor);
        }
      }
      
      return count($a_tmp)>0?$a_tmp:null;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  public function selectNumRows()
  {
    try
    {
      $where = $this->buildConsult();

      $o_conexion = new BaseDatos();
      $con = $o_conexion->conectar();
      $sql = "SELECT dlv_id, dlv_tipo, dlv_dlvNum, dlv_almId, dlv_clienteId, dlv_clienteNombre, dlv_contacto, dlv_docReferNum, dlv_facturaNum, dlv_moneda, dlv_fechaRequerida, dlv_fechaProgramada, dlv_fechaSalida, dlv_flete, dlv_totalBruto, dlv_neto, dlv_obs, dlv_status, dlv_creaUsr, dlv_creaFecha, dlv_modifUsr, dlv_modifFecha FROM so_delivery WHERE 1 ".$where;
        $result = mysql_query($sql);
      $num_rows = mysql_num_rows($result);
      
      return $num_rows;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  public function buildConsult()
  {
    try
    {
      $where = "";

      if($this->getId()){ $where .= " AND dlv_id = '".$this->getId()."'"; }
      if($this->getTipo()){ $where .= " AND dlv_tipo = '".$this->getTipo()."'"; }
      if($this->getDlvNum()){ $where .= " AND dlv_dlvnum = '".$this->getDlvNum()."'"; }
      if($this->getAlmId()){ $where .= " AND dlv_almid = '".$this->getAlmId()."'"; }
      if($this->getClienteId()){ $where .= " AND dlv_clienteid = '".$this->getClienteId()."'"; }
      if($this->getClienteNombre()){ $where .= " AND dlv_clientenombre = '".$this->getClienteNombre()."'"; }
      if($this->getContacto()){ $where .= " AND dlv_contacto = '".$this->getContacto()."'"; }
      if($this->getDocReferNum()){ $where .= " AND dlv_docrefernum = '".$this->getDocReferNum()."'"; }
      if($this->getFacturaNum()){ $where .= " AND dlv_facturanum = '".$this->getFacturaNum()."'"; }
      if($this->getMoneda()){ $where .= " AND dlv_moneda = '".$this->getMoneda()."'"; }
      if($this->getFechaRequerida()){ $where .= " AND dlv_fecharequerida = '".$this->getFechaRequerida()."'"; }
      if($this->getFechaProgramada()){ $where .= " AND dlv_fechaprogramada = '".$this->getFechaProgramada()."'"; }
      if($this->getFechaSalida()){ $where .= " AND dlv_fechasalida = '".$this->getFechaSalida()."'"; }
      if($this->getFlete()){ $where .= " AND dlv_flete = '".$this->getFlete()."'"; }
      if($this->getTotalBruto()){ $where .= " AND dlv_totalbruto = '".$this->getTotalBruto()."'"; }
      if($this->getNeto()){ $where .= " AND dlv_neto = '".$this->getNeto()."'"; }
      if($this->getObs()){ $where .= " AND dlv_obs = '".$this->getObs()."'"; }
      if($this->getStatus()){ $where .= " AND dlv_status = '".$this->getStatus()."'"; }
      if($this->getCreaUsr()){ $where .= " AND dlv_creausr = '".$this->getCreaUsr()."'"; }
      if($this->getCreaFecha()){ $where .= " AND dlv_creafecha = '".$this->getCreaFecha()."'"; }
      if($this->getModifUsr()){ $where .= " AND dlv_modifusr = '".$this->getModifUsr()."'"; }
      if($this->getModifFecha()){ $where .= " AND dlv_modiffecha = '".$this->getModifFecha()."'"; }
      return $where;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  public function buildOrderByField()
  {
    try
    {
      $order_by = "";

      if(property_exists($this,'sortCampos')){
      if($this->sortCampos != ""){
        $a_valCampos = explode(";", $this->sortCampos);
        $a_valtype   = explode(";", $this->sortType);
        $order_by = " ORDER BY ";
        $a_order = array();
        foreach ($a_valCampos as $key => $val){
          $sTmpOrder = "";
          if(array_key_exists($val, self::$fieldNames)){
            $sTmpOrder .= self::$fieldNames[$val];
            switch ($a_valtype[$key]){
              case 1: $sTmpOrder .= " ASC"; break;
              case 2: $sTmpOrder .= " DESC"; break;
            }
            $a_order[] = $sTmpOrder;
          }
        }
        $order_by .= implode(", ", $a_order);
      }
      }
      return $order_by;
    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  /* B�squeda del registro por la llave primaria */
  public function selectByPK()
  {
    try
    {
      $o_conexion = new BaseDatos();
      $con = $o_conexion->conectar();
      $sql = "SELECT dlv_id, dlv_tipo, dlv_dlvNum, dlv_almId, dlv_clienteId, dlv_clienteNombre, dlv_contacto, dlv_docReferNum, dlv_facturaNum, dlv_moneda, dlv_fechaRequerida, dlv_fechaProgramada, dlv_fechaSalida, dlv_flete, dlv_totalBruto, dlv_neto, dlv_obs, dlv_status, dlv_creaUsr, dlv_creaFecha, dlv_modifUsr, dlv_modifFecha FROM so_delivery WHERE 1 AND dlv_id='".$this->getId()."'; ";
        $result = mysql_query($sql);
      $o_Propel = $result ? mysql_fetch_object($result) : null;
      

      return $o_Propel?$this->insertObjectBN($o_Propel):null;

    }catch ( Exception $o_Error ){
      throw new Exception($o_Error->getMessage().' en '.$o_Error->getFile().' linea '.$o_Error->getLine());
    }
  }

  /* Devuelve un objeto con m�todos de acceso */
  private function insertObjectBN($o_propel){
    $o_BaseSoDeliveryFN = new SoDeliveryBN();
    $o_BaseSoDeliveryFN->setId($o_propel->dlv_id);
    $o_BaseSoDeliveryFN->setTipo($o_propel->dlv_tipo);
    $o_BaseSoDeliveryFN->setDlvNum($o_propel->dlv_dlvNum);
    $o_BaseSoDeliveryFN->setAlmId($o_propel->dlv_almId);
    $o_BaseSoDeliveryFN->setClienteId($o_propel->dlv_clienteId);
    $o_BaseSoDeliveryFN->setClienteNombre($o_propel->dlv_clienteNombre);
    $o_BaseSoDeliveryFN->setContacto($o_propel->dlv_contacto);
    $o_BaseSoDeliveryFN->setDocReferNum($o_propel->dlv_docReferNum);
    $o_BaseSoDeliveryFN->setFacturaNum($o_propel->dlv_facturaNum);
    $o_BaseSoDeliveryFN->setMoneda($o_propel->dlv_moneda);
    $o_BaseSoDeliveryFN->setFechaRequerida($o_propel->dlv_fechaRequerida);
    $o_BaseSoDeliveryFN->setFechaProgramada($o_propel->dlv_fechaProgramada);
    $o_BaseSoDeliveryFN->setFechaSalida($o_propel->dlv_fechaSalida);
    $o_BaseSoDeliveryFN->setFlete($o_propel->dlv_flete);
    $o_BaseSoDeliveryFN->setTotalBruto($o_propel->dlv_totalBruto);
    $o_BaseSoDeliveryFN->setNeto($o_propel->dlv_neto);
    $o_BaseSoDeliveryFN->setObs($o_propel->dlv_obs);
    $o_BaseSoDeliveryFN->setStatus($o_propel->dlv_status);
    $o_BaseSoDeliveryFN->setCreaUsr($o_propel->dlv_creaUsr);
    $o_BaseSoDeliveryFN->setCreaFecha($o_propel->dlv_creaFecha);
    $o_BaseSoDeliveryFN->setModifUsr($o_propel->dlv_modifUsr);
    $o_BaseSoDeliveryFN->setModifFecha($o_propel->dlv_modifFecha);
    return $o_BaseSoDeliveryFN;
  }
}